package ibm.cognos.imt.template

import groovy.xml.MarkupBuilder

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 5/12/14
 * Time: 1:47 PM
 * To change this template use File | Settings | File Templates.
 */
class FilterTemplate extends AbstractTemplate{
    def filter
    FilterTemplate(f) {
        filter = f
    }

    @Override
    def generateTemplateSpec(MarkupBuilder builder) {
        builder.detailFilter(postAutoAggregation:"true", use:"required"){
            filterDefinition ()
//                    {
//                if (compare != null) {
//                    filterCompare(refDataItem:"${f.refdataItem.name}", dataType:"${dataType()}", operator:"$compare") {
//                        filterValue(values[0])
//                    }
//                }
//                if(range!=null){
//                    filterRange(refDataItem: "${dataItem.name}", dataType:"${dataType()}") {
//                        filterFrom("${values[0]}")
//                        filterTo("${values[1]}")
//                    }
//                }
//                if (inValues != null) {
//                    filterInValues(refDataItem: "${dataItem.name}", dataType:"${dataType()}") {
//                        filterValues {
//                            for (value in values) {
//                                filterValue("$value")
//                            }
//                        }
//                    }
//                }
//
//            }
        }
    }

    @Override
    def getTemplateNode() {
        root = new XmlSlurper().parseText(getSpecStr())
        if (filter.range != null) {
            def filterCompareNode = new XmlSlurper().parseText("<filterCompare><filterValue><filterValue></filterCompare>")
            root.filterDefinition.appendNode(filterCompareNode)
        }
        if (filter.compare != null) {
            def filterRangeNode = new XmlSlurper().parseText("<filterRange><filterFrom></filterFrom><filterTo></filterTo></filterRange>")
            root.filterDefinition.appendNode(filterRangeNode)
        }
        if (filter.inValues != null) {
            def filterInValuesNode = new XmlSlurper().parseText("<filterInValues><filterValue></filterVallue></filterInValues>")
            root.filterDefinition.appendNode(filterInValuesNode)
        }
        root
    }
}
